package com.gugee.tiktok.data.consumer.music.core.spider;


import com.gugee.tiktok.common.model.spider.aweme.BloggerAwemesResponse;
import com.gugee.tiktok.common.model.spider.blogger.BloggerResponse;
import com.gugee.tiktok.common.model.spider.music.MusicResponse;
import com.xiguaji.boot.retry.RetryableException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

@Component
public class MusicSpiderClient{
    @Autowired
    RestTemplate restTemplate;

    @Value("${xiguaji.spider.host}")
    String host;

    @Retryable(value = RuntimeException.class, maxAttempts = 10)
    public MusicResponse get(long musicId){
        String uri = host + "/Aweme/MusicDetail?music_id={musicId}";
        ResponseEntity<MusicResponse> responseEntity = restTemplate.getForEntity(uri,MusicResponse.class,musicId);

        if(responseEntity.getStatusCode() != HttpStatus.OK) {
            throw new RetryableException(String.format("search music error. status: %s . msg: %s uri: %s", responseEntity.getStatusCodeValue(), responseEntity.getBody(),uri.replace("{musicId}",String.valueOf(musicId))));
        }

        if(responseEntity.getBody() == null ){
            throw new RetryableException(String.format("search music error. status: %s . msg: %s uri: %s", responseEntity.getStatusCodeValue(), responseEntity.getBody(),uri.replace("{musicId}",String.valueOf(musicId))));

        }

        if(responseEntity.getBody().getMusic_info() == null || responseEntity.getBody().getMusic_info().getUser_count() == 0){
            throw new RetryableException(String.format("search music error. status: %s . msg: %s uri: %s", responseEntity.getStatusCodeValue(), responseEntity.getBody(),uri.replace("{musicId}",String.valueOf(musicId))));
        }
        return responseEntity.getBody();
    }

    @Retryable(value = RuntimeException.class, maxAttempts = 10)
    public BloggerAwemesResponse getAwemes(long musicId,int count) {
        String uri = host + "/Aweme/MusicAweme?music_id={musicId}&count={count}";
        ResponseEntity<BloggerAwemesResponse> responseEntity = restTemplate.getForEntity(uri, BloggerAwemesResponse.class, musicId,count);
        if (responseEntity.getStatusCode() != HttpStatus.OK) {
            throw new RetryableException(String.format("get music awemes error. status: %s . msg: %s uri: %s", responseEntity.getStatusCodeValue(), responseEntity.getBody(),uri.replace("{musicId}",String.valueOf(musicId)).replace("{count}",String.valueOf(count))));
        }
        if (responseEntity.getBody() == null) {
            throw new RetryableException("get music awemes error, retry times 10. uri:" + uri.replace("{musicId}",String.valueOf(musicId)).replace("{count}",String.valueOf(count)));
        }
        if (responseEntity.getBody().getAweme_list() == null) {
            throw new RetryableException("get music awemes error, retry times 10. uri:" + uri.replace("{musicId}",String.valueOf(musicId)).replace("{count}",String.valueOf(count)));
        }
        return responseEntity.getBody();
    }

    @Retryable(value = RuntimeException.class, maxAttempts = 10)
    public BloggerResponse getBlogger(Long uid, String region) {
        String uri = host + "/aweme/UserProfileFromUid?user_id={user_id}&region={region}";
        if (StringUtils.isEmpty(region)) {
            region = "US";
        }

        ResponseEntity<BloggerResponse> responseEntity = restTemplate.getForEntity(uri, BloggerResponse.class, uid, region);
        if (responseEntity.getStatusCode() != HttpStatus.OK) {
            throw new RetryableException(String.format("get blogger error. status: %s . msg: %s", responseEntity.getStatusCodeValue(), responseEntity.getBody()));
        }
        if (responseEntity.getBody() == null || responseEntity.getBody().getStatus_code() == 3071) {

            throw new RetryableException("get blogger error, retry times 10. uri:" +
                    uri.replace("{user_id}", uid.toString()).replace("{region}", region));
        }
        return responseEntity.getBody();
    }
}